//
//  CoachInfoViewController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/14.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//


typedef NS_ENUM(NSInteger,CoachInfoVCCellType) {
    CoachInfoVCCellType_Name,//姓名
    CoachInfoVCCellType_Sex,//性别
    CoachInfoVCCellType_Title,//职位
    CoachInfoVCCellType_Mobile,//手机
    CoachInfoVCCellType_Gym,//所在门店
};

#import "CoachInfoViewController.h"
#import "CommonTableViewCell.h"
#import "I_Coach.h"
#import "GroupRootViewController.h"

@interface CoachInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;
@property (nonatomic, strong) M_Coach *coach;//
@property (nonatomic, strong) NSArray *dataSource;

@end

@implementation CoachInfoViewController

+ (instancetype)controllerWithCoach:(M_Coach *)coach
{
    CoachInfoViewController *vc = [UIStoryboard controllerWithStoryBoardIdentify:NSStringFromClass([CoachInfoViewController class]) inStoryBoard:@"Group"];
    vc.coach = coach;
    return vc;
}


#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"教练信息";
    [self configUI];
    _dataSource = @[@[@(CoachInfoVCCellType_Name),@(CoachInfoVCCellType_Sex),@(CoachInfoVCCellType_Title),@(CoachInfoVCCellType_Mobile),@(CoachInfoVCCellType_Gym)]];
}

#pragma mark UI
- (void)configUI
{

    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = TP.controllerBackColor;
    self.tableView.insetTop = 10;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[CommonTableViewCell class] forCellReuseIdentifier:NSStringFromClass([CommonTableViewCell class])];
    _deleteBtn.layer.cornerRadius = 2;
    _deleteBtn.layer.masksToBounds = YES;
}


#pragma mark Action

- (IBAction)deleteBtnClicked:(id)sender {
    WeakSelf(self);
    [YDAlertViewController alertViewControllerWithMessage:@"解除关联后教练所属会员信息将不再统计到报表中,确定解除与该教练的关联？" cancleTitle:@"取消" confimTitle:@"立即解除" cancleBlock:nil confimBlock:^{
        [I_Coach deleteCoachWithCoachId:weakself.coach.coachId success:^{
            [AV showSuccess:@"解除成功"];
            NSArray *viewControllers = weakself.navigationController.viewControllers;
            BOOL find = NO;
            for (UIViewController *vc  in viewControllers) {
                if ([vc isKindOfClass:[GroupRootViewController class]]) {
                    [weakself.navigationController popToViewController:vc animated:YES];
                    find = YES;
                    break;
                }
            }
            if (!find) {
                [weakself.navigationController popViewControllerAnimated:YES];
            }
        } failure:^(NSError *error) {
            [AV showError:error];
        }];
    }];
}


#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = _dataSource[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CommonTableViewCell class])];
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    CoachInfoVCCellType cellType = [_dataSource[indexPath.section][indexPath.row] integerValue];
    cell.showStyle = CommonTableViewShowStyleRightText;
    switch (cellType) {
        case CoachInfoVCCellType_Name:
        {
            cell.leftLabel.text = @"姓名";
            cell.rightLabel.text = _coach.coachName;
        }
            break;
        case CoachInfoVCCellType_Sex:
        {
            cell.leftLabel.text = @"性别";
            cell.rightLabel.text = _coach.coachSex == CoachSexType_Female ? @"女" : @"男";;
        }
            break;
        case CoachInfoVCCellType_Title:
        {
            cell.leftLabel.text = @"职位";
            cell.rightLabel.text =  [NSString stringWithFormat:@"%@",_coach.coachTitle];
        }
            break;
        case CoachInfoVCCellType_Mobile:
        {
            cell.leftLabel.text = @"联系电话";
            cell.rightLabel.text = _coach.coachMobile ? : @"未设置";
        }
            break;

        case CoachInfoVCCellType_Gym:
        {
            cell.leftLabel.text = @"所在门店";
            cell.rightLabel.text = RI.currentGym.gymName;
        }
            break;
            
        default:
            break;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}
@end
